package com.wind.bdg.middlewarelearndemo.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class Url2Text {
	public static void main(String[] args) throws Exception {
		App app = new App();
		List listTitle = app.getTitle("<td>《*[0-9]*[\\u4e00-\\u9fa5]+.*?</td>");
		List listTime = app.getTime("<td>2020-.*?</td>");
		List listUrl = app.getUrl("http://10.202.32.15/medicine/medicine/webNewsList.do?id=505798913");
		List<String[]> list = new ArrayList<String[]>();
		for(int i=0;i<listTitle.size();i++) {
			String[] tem = new String[3];
			tem[0]=(String) listUrl.get(i);
			tem[1]=(String) listTitle.get(i);
			tem[2]=(String) listTime.get(i);
			list.add(tem);
		}
		
		for(int i=0;i<list.size();i++) {
			String time = list.get(i)[2];
//			System.out.println(time);
			mkTxt(time);
			String content = list.get(i)[0].replaceAll(".*?id=","")+list.get(i)[1]+list.get(i)[2];
			String filePath = "C:\\Users\\Administrator\\Desktop\\ListNews"+time+".txt";
			write2txt(content, filePath);
		}
		
	}
	//创建文件
	public static void mkTxt(String time) throws Exception {
		
				String fileName ="ListNews"+time+".txt";
		        File testFile = new File("C:\\Users\\Administrator\\Desktop\\" + fileName);
		        if (!testFile.exists()) {
		            testFile.createNewFile();// 能创建多级目录
//		            System.out.println("ok");
		        }
	}
	//写内容到txt
	public static void write2txt(String content,String filePath) {
        FileWriter fwriter = null;
        try {
            // true表示不覆盖原来的内容，而是加到文件的后面。若要覆盖原来的内容，直接省略这个参数就好
            fwriter = new FileWriter(filePath,true);
            fwriter.write(content);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                fwriter.flush();
                fwriter.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
